<template>
<!--pages/updatePwd/updatePwd.wxml-->
<view>
  <view class="form">
    <view class="form_item">
      <label>+86</label>
      <input type="text" placeholder="请输入手机号" v-model="phone" @focus='clearSta'/>
	  <view style="color: red; font-size: 22rpx;margin-top: 20rpx;" v-if="status==1" >号码不规范</view>
    </view>
    <view class="codes">
      <view class="code">
        <view></view>
        <label style="width:198rpx">验证码</label>
        <input type="text" v-model="Code" style="width:314rpx" placeholder="请输入验证码" />
      </view>
      <view @click="getCode"><button class="btn" style="width:200rpx;height:84rpx;line-height:84rpx;"><text>{{codeText}}</text></button></view>
    </view>
    <view class="form_item">
      <label>密码</label>
      <input type="password" placeholder="请输入密码" v-model="pwd" @focus="clearSta"/>
	  <view style="color: red; font-size: 22rpx;margin-top: 20rpx;" v-if="status==2" >请输入密码</view>
    </view>
    <button style="width:560rpx" class="submit" @click="goReset"><text>确认重置</text></button>
  </view>
</view>
</template>

<script>
	import {RetrievePwd,getPwdCode} from '@/api/index.js'
let timeNum = 60; //60秒倒计时
//60秒倒计时
let countDownTime = timeNum;
let timer; //计时器

export default {
  data() {
    return {
      codeText: "获取验证码",
      codeColor: "",
	  phone:'',
	  status:0,
	  pwd:'',
	  flag:true,
	  Code:''
    };
  },

  components: {},
  props: {},
  methods: {
    //获取验证码
    getCode() {
		if(this.phone=='') {this.status=1; return} 
		
      if (countDownTime == timeNum) {
        uni.showToast({
          title: '验证码已发送',
          icon: "success"
        });
		getPwdCode(this.phone).then(res=>{
			console.log(res)
		})
        this.setInterval();
        
			this.codeText ='剩余' + countDownTime.toString().padStart(2,'0')+'s'
      } else {
        uni.showToast({
          title: '请等待验证码',
          icon: "none",
		  status:true
        });
      }
    },

    // 计时器
    setInterval: function () {
      const that = this;
      timer = setInterval(function () {
        // 设置定时器
        countDownTime--;

        if (countDownTime < 1) {
          clearInterval(timer);
          // that.setData({
          //   codeText: "获取验证码"
          // });
		  that.codeText = '获取验证码'
          countDownTime = timeNum;
        } else {
          // that.setData({
          //   codeText: countDownTime + "s"
          // });
		  that.codeText ='剩余' + countDownTime.toString().padStart(2,'0')+'s'
        } 

      }, 1000);
    },
    goReset () {
		let _this =this
		if(!this.flag) return
		console.log("xxx")
		let myreg = /^[1][1,3,2,4,5,7,8,9][0-9]{9}$/;
		if(!myreg.test(this.phone)){
			this.status =1
			return
		}
		if(this.pwd==''){
			this.status=2
			return
		}
		if(this.Code==''){
			uni.showToast({
				title:'验证码为空',
				icon:'none'
			})
			return
		}
		let data = {
			phoneNumber:this.phone,
			password:this.pwd
		}
		// let code='123'
		// console.log(_this.code)
		RetrievePwd(data,this.Code).then(res=>{
			if(res[1].data.code==200){
				uni.showToast({
					icon:'success',
					title:'修改成功'
				})
				setTimeout(()=>{
					uni.navigateTo({
						url:'/pages/login/login'
					})
				},1000)
			}
			// uni.showToast({
			// 	icon:'none',
			// 	title:res[0].errMsg
			// })
			console.log(res)
		}).catch(err=>{
			if(err){
				uni.showToast({
					icon:'none',
					title:'网络错误'
				})
			}
			
		})
		this.flag = false
		let number =60
		let t = setInterval(()=>{
			number -- 
		},1000)
		if(number<=1){
			clearInterval(t)
			_this.flag = true
			number = 60
		}
      // uni.navigateBack({
      //   delta: 1
      // });
    },
	//得到焦点
	clearSta(){
		this.status = false
	}
	
  }
};
</script>
<style>
@import "./updatePwd.css";
</style>